package com.gorkr.labuladong.data_struct.array;

import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.PriorityQueue;

/**
 * @author gorkr
 * @date 2022/06/08 10:11
 **/
public class M870AdvantageShuffle {
    public int[] advantageCount(int[] nums1, int[] nums2) {
        Arrays.sort(nums1);
        int left = 0, right = nums1.length - 1;

        PriorityQueue<int[]> pq = new PriorityQueue<>(
                (int[] pair1, int[] pair2) -> {
                    return pair2[1] - pair1[1];
                }
        );
        for (int i = 0; i < nums2.length; i++) {
            pq.offer(new int[]{i, nums2[i]});
        }
        int[] res = new int[nums1.length];

        while (!pq.isEmpty()) {
            int[] pair = pq.poll();
            int i = pair[0], restMaxVal = pair[1];
            if (nums1[right] > restMaxVal) {
                res[i] = nums1[right];
                right--;
            } else {
                res[i] = nums1[left];
                left++;
            }

        }

        return res;

    }
}
    